package com.xy.controller.uc;

import com.xy.common.base.ApiBaseResponse;
import com.xy.common.base.BaseController;
import com.xy.entity.User;
import com.xy.repository.PictureRespository;
import com.xy.repository.UserRepository;
import com.xy.util.UpPictureUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Part;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;


/**
 * Created with IntelliJ IDEA.
 * Description:个人信息
 * Date: 2018-01-16
 * Time: 17:39
 */
@Api(value = "个人信息Controller", description = "个人信息")
@RestController
@RequestMapping(value = "api/user")
public class UserMessageController extends BaseController {
    @Autowired
    private UserRepository userRepository;
    @Autowired
    private PictureRespository pictureRespository;

    @ApiOperation("个人信息")
    @GetMapping("/info")
    @ResponseStatus(HttpStatus.OK)
    private ResponseEntity<?> Allpicture() {
        initBaseInfo();
        User user = new User();
        user = userRepository.findOne(userId);
        return ResponseEntity.ok(ApiBaseResponse.fromHttpStatus(HttpStatus.OK, user));
    }

    @ApiOperation("修改头像")
    @PostMapping("/update/head")
    @ResponseStatus(HttpStatus.OK)
    public ResponseEntity<?> updateHead(@RequestPart("part") Part part) throws IOException {
        initBaseInfo();
        UpPictureUtil upPictureUtil = new UpPictureUtil();
        Map<String, String> map = new HashMap<>();
        map = upPictureUtil.upPicture(part);
        String result = map.get("result");
        if (result.equals("1")) {
            String url = map.get("url");
            userRepository.updateHead(url, userId);
        }
        User user = new User();
        user = userRepository.findOne(userId);
        return ResponseEntity.ok(ApiBaseResponse.fromHttpStatus(HttpStatus.OK, user));
    }


    @ApiOperation("修改昵称")
    @PutMapping("/update/nickname/{nickname}")
    @ResponseStatus(HttpStatus.OK)
    public ResponseEntity<?> updateNickname(@PathVariable("nickname") String nickname) {
        initBaseInfo();
        userRepository.updateNickname(nickname, userId);
        User user = new User();
        user = userRepository.findOne(userId);
        return ResponseEntity.ok(ApiBaseResponse.fromHttpStatus(HttpStatus.OK, user));
    }


    @ApiOperation("修改手机号")
    @PutMapping("/update/phone/{phone}")
    @ResponseStatus(HttpStatus.OK)
    public ResponseEntity<?> updatePhone(@PathVariable("phone") String phone) {
        initBaseInfo();

        userRepository.updatePhone(phone, userId);

        User user = new User();
        user = userRepository.findOne(userId);
        return ResponseEntity.ok(ApiBaseResponse.fromHttpStatus(HttpStatus.OK, user));
    }

    @ApiOperation(value = "修改性别", notes = "0女1男")
    @PutMapping("/update/gender/{gender}")
    @ResponseStatus(HttpStatus.OK)
    public ResponseEntity<?> updateGender(@PathVariable(value = "gender") String gender) {
        initBaseInfo();
        userRepository.updateGender(gender, userId);
        User user = new User();
        user = userRepository.findOne(userId);
        return ResponseEntity.ok(ApiBaseResponse.fromHttpStatus(HttpStatus.OK, user));
    }

    @ApiOperation("修改出生年月")
    @PutMapping("/update/birth/{birth}")
    @ResponseStatus(HttpStatus.OK)
    public ResponseEntity<?> updateBirth(@PathVariable(value = "birth") String date) {

        initBaseInfo();
        java.sql.Date birth = java.sql.Date.valueOf(date);
        int a = userRepository.updateBirth(birth, userId);
        User user = new User();
        user = userRepository.findOne(userId);
        return ResponseEntity.ok(ApiBaseResponse.fromHttpStatus(HttpStatus.OK, user));
    }


}
